package myHomework;
import java.util.HashMap;
import java.util.Map;

public class Leetcode290 {
    public static void main(String[] args) {
        boolean result= wordPattern("abba","dog cat cat dog");
        System.out.println(result);
    }
    public static boolean wordPattern(String pattern, String s) {
        String[] word=s.split(" ");
        Map<Character,String> patternToS=new HashMap<>();
        Map<String,Character> sToPattern=new HashMap<>();
        int i;
        for(i=0;i<pattern.length();i++)
        {
            char a=pattern.charAt(i);
            String b=word[i];
            if(patternToS.containsKey(a)&&!patternToS.get(a).equals(b)||
                    sToPattern.containsKey(b)&&!sToPattern.get(b).equals(a)){
                return false;
            }
            patternToS.put(a,b);
            sToPattern.put(b,a);
        }
        return true;
    }
}


/*
class Solution {
    public boolean wordPattern(String pattern, String s) {
        String[] word=s.split(" ");
        Map<Character,String> patternToS=new HashMap<>();
        Map<String,Character> sToPattern=new HashMap<>();
        int i;
        for(i=0;i<pattern.length();i++)
        {
            char a=pattern.charAt(i);
            String b=word[i];
            if(patternToS.containsKey(a)&&patternToS.get(a)!=b||sToPattern.containsKey(b)&&sToPattern.get(b)!=a){
                return false;
            }
            patternToS.put(a,b);
            sToPattern.put(b,a);
        }
        return true;
    }
}*/
